package com.swj.demo.aop;


import com.microservice.common.R;
import com.microservice.common.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

/**
 * write something please~
 *
 * @author huangrz
 * @since 2021/9/17
 **/
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 参数校验异常（约束校验）
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(ConstraintViolationException.class)
    public R<?> handleValidationException(ConstraintViolationException e) {
        log.error("参数校验不通过: " + e.getMessage(), e);
        StringBuilder message = new StringBuilder();
        for (ConstraintViolation<?> cv : e.getConstraintViolations()) {
            message.append("[").append(cv.getMessage()).append("]");
        }
        return R.fail(message.toString());
    }

    /**
     * 数据绑定异常（数据类型校验）
     */
    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.OK)
    public R<?> handleBindException(BindException e) {
        log.error("参数绑定异常: " + e.getMessage(), e);
        StringBuilder message = new StringBuilder();
        for (FieldError error : e.getFieldErrors()) {
            message.append("[").append(error.getDefaultMessage()).append("]");
        }
        return R.fail(message.toString());
    }

    /**
     * 文件上传解析异常
     */
    @ExceptionHandler(MultipartException.class)
    @ResponseStatus(HttpStatus.OK)
    public R<?> handleMultipartException(MultipartException e){
        log.error("多媒体文件解析异常：" + e.getMessage(), e);
        return R.fail("文件解析失败！");
    }

    /**
     * 非法参数异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.OK)
    public R<?> handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("非法参数: " + e.getMessage(), e);
        return R.fail("非法参数：[" + e.getMessage() + "]");
    }

    /**
     * 请求方法不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    public R<?> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("非法请求方法: " + e.getMessage(), e);
        return R.fail("非法请求方法！");
    }

    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(HttpStatus.OK)
    public R<?> handleServiceException(ServiceException e) {
        return R.fail(e.getMsg());
    }

    /**
     * 其余异常统一处理
     */
    @ExceptionHandler(Exception.class)
    public R<?> handleException(Exception e) {
        log.error("未知异常: " + e.getMessage(), e);
        return R.fail("未知错误！");
    }

}
